﻿#include "CloudDevice.h"
//#include "Device/Zigbee3Device.h"
#include <unistd.h>
CloudDevice::CloudDevice(QObject *parent) :
    BaseInterface<CloudDevice>(parent)
{
    qDebug()<<"CloudDevice running-1";
    m_pInstance = this;
    m_isLoginSucceed = false;
    cloudProtocol = new CloudProtocol(this);

    this->registerProtocol(cloudProtocol);

    heartbeatTimer = new QTimer(this);
    connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(heartbeatSlot()));


    this->setIsDecrypt(true);
    this->setIsEncrypt(true);

    registerCmdHandler(ResponseCmd::FunctionCode,ResponseCmd::staticMetaObject, &CloudDevice::responseCmdHandle);
    registerCmdHandler(HandshakeSucceedCmd::FunctionCode,HandshakeSucceedCmd::staticMetaObject, &CloudDevice::handshakeSucceedCmdHandle);
    registerCmdHandler(HeartbeatCmd::FunctionCode,HeartbeatCmd::staticMetaObject, &CloudDevice::heartBeatCmdHandle);
    registerCmdHandler(GatewayLoginSucceedCmd::FunctionCode,GatewayLoginSucceedCmd::staticMetaObject, &CloudDevice::gatewayLoginSucceedCmdHandle);
    registerCmdHandler(DownloadPointListCmd::FunctionCode,DownloadPointListCmd::staticMetaObject, &CloudDevice::downloadPointListCmdHandle);
    registerCmdHandler(DownloadCustomSceneCmd::FunctionCode,DownloadCustomSceneCmd::staticMetaObject, &CloudDevice::downloadCustomSceneCmdHandle);
    registerCmdHandler(UpdateCustomSceneCmd::FunctionCode,UpdateCustomSceneCmd::staticMetaObject, &CloudDevice::updateCustomSceneCmdHandle);
    registerCmdHandler(CloudControlPointCmd::FunctionCode,CloudControlPointCmd::staticMetaObject, &CloudDevice::controlPointCmdHandle);
    registerCmdHandler(CustomSceneControlCmd::FunctionCode,CustomSceneControlCmd::staticMetaObject, &CloudDevice::customSceneControlCmdHandle);
    registerCmdHandler(DownloadAutomaticCmd::FunctionCode,DownloadAutomaticCmd::staticMetaObject, &CloudDevice::downloadAutomaticCmdHandle);
    registerCmdHandler(UpdateAutomaticCmd::FunctionCode,UpdateAutomaticCmd::staticMetaObject, &CloudDevice::updateAutomaticCmdHandle);
    registerCmdHandler(UpdatePointCmd::FunctionCode,UpdatePointCmd::staticMetaObject, &CloudDevice::updatePointCmdHandle);
    registerCmdHandler(DownloadFileCmd::FunctionCode,DownloadFileCmd::staticMetaObject, &CloudDevice::downloadFileCmdHandle);
    registerCmdHandler(RebootCmd::FunctionCode,RebootCmd::staticMetaObject, &CloudDevice::rebootCmdHandle);
    registerCmdHandler(DownloadLogFile::FunctionCode,DownloadLogFile::staticMetaObject, &CloudDevice::downloadLogFileCmdHandle);
//    registerCmdHandler(HeartBestCmd::FunctionCode,HeartBestCmd::staticMetaObject, &CloudDevice::heartBestHandle);

    registerCmdHandler(HeartBestCmd::FunctionCode,HeartBestCmd::staticMetaObject, &CloudDevice::heartBestHandled);
    registerCmdTimeOUTHandler(HeartbeatCmd::FunctionCode,&CloudDevice::heartbeatCmdTimeoutHandle);

    registerFuncHandler(UpdatePointFunc::FunctionCode, &CloudDevice::updatePointFuncHandle);
    registerFuncHandler(DownloadFileResultFunc::FunctionCode, &CloudDevice::downloadFileResultFuncHandle);
  //  registerFuncHandler(RS485setTime::FunctionCode, &CloudDevice::downloadFileResultFuncHandle);

   qDebug()<<"CloudDevice running-2";
}



void CloudDevice::isConnectedSlot(bool isConnect,PortStruct target)
{

    if(isConnect)
    {
        m_target = target;
        HandshakeCmd *handshakeCmd = new HandshakeCmd();
        Encrypt *encrypt = Encrypt::getInstance();
        QByteArray clientRandom = encrypt->generateRandom(16);
        QByteArray ivecRandom   = encrypt->generateRandom(16);

        handshakeCmd->setIsResend(true);
        handshakeCmd->setReSendCount(3);
        handshakeCmd->setTimeoutCount(3000);

        handshakeCmd->setClientRandom(clientRandom);
        handshakeCmd->setIvecRandom(ivecRandom);
        System *system = System::getInstance();
        handshakeCmd->setAppVersion(system->appVersion());
        handshakeCmd->setSystemName(system->systemName());
        handshakeCmd->setSystemVersion(system->systemVersion());
        handshakeCmd->setClientType(system->deviceType());
        QSharedPointer<BaseCmd> sharedHandshakeCmd(handshakeCmd);
        encrypt->setAesKey(clientRandom,ivecRandom);

        this->sendCmd(m_target,sharedHandshakeCmd);
    }
}

void CloudDevice::heartbeatSlot()
{

    if(!m_isLoginSucceed)
    {
        return;
    }
    HeartbeatCmd *heartBeatCmd = new HeartbeatCmd();
    heartBeatCmd->setIsResend(true);
    heartBeatCmd->setReSendCount(m_heartbeatTimeoutCount);
    heartBeatCmd->setTimeoutCount(m_heartbeatTimeoutTime);
    QSharedPointer<BaseCmd> sharedHeartBeatCmd(heartBeatCmd);
    this->sendCmd(m_target,sharedHeartBeatCmd);
    AutomaticXml *automaticXml = AutomaticXml::getInstance();
    automaticXml->setBestStata(true);

    StateSyncCmd *stateSyncCmd = new StateSyncCmd();
    System *system = System::getInstance();
    stateSyncCmd->setState(0);
    stateSyncCmd->setUsedRamSize(system->usedRamSize());
    stateSyncCmd->setTotalRamSize(system->totalRamSize());
    stateSyncCmd->setUsedFlashSize(system->usedFlashSize());
    stateSyncCmd->setTotalFlashSize(system->totalFlashSize());
    stateSyncCmd->setRunningTime(system->runningTime());
    stateSyncCmd->setIp(system->ip());
    stateSyncCmd->setAppVersion(system->appVersion());
    QSharedPointer<BaseCmd> sharedSyncCmd(stateSyncCmd);
    this->sendCmd(m_target,sharedSyncCmd);
}

void CloudDevice::responseCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    ResponseCmd *responseCmd = qobject_cast<ResponseCmd*>(sharedCmd.data());
    quint16 responseCode = responseCmd->responseCode();
    if(responseCode != 0)
    {
        reconnectTcpSrever();
    }
}


void CloudDevice::handshakeSucceedCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    HandshakeSucceedCmd *handshakeSucceedCmd = qobject_cast<HandshakeSucceedCmd*>(sharedCmd.data());
    Encrypt *encrypt = Encrypt::getInstance();
    QByteArray serverRandom = handshakeSucceedCmd->serverRandom();
    QByteArray clientRandom;
    QByteArray ivec;   
    encrypt->getAesKey(clientRandom,ivec);
    QByteArray aesKey = encrypt->mixAesKey(clientRandom,serverRandom);
    encrypt->setAesKey(aesKey,ivec);

    m_heartbeatIntervalTime = handshakeSucceedCmd->heartbeatIntervalTime();
    m_heartbeatTimeoutCount = handshakeSucceedCmd->heartbeatTimeoutCount();
    m_heartbeatTimeoutTime  = handshakeSucceedCmd->heartbeatTimeoutTime();
    QByteArray serverTime   = handshakeSucceedCmd->serverTime();

    System *system = System::getInstance();
    system->setTimeBCD(serverTime);

    GatewayLoginCmd *gatewayLoginCmd = new GatewayLoginCmd();
    MacXml *macXml = MacXml::getInstance();
    gatewayLoginCmd->setLoginTime(system->timeBCD());
    gatewayLoginCmd->setMac(macXml->mac());
    gatewayLoginCmd->setIsResend(true);
    gatewayLoginCmd->setReSendCount(3);
    gatewayLoginCmd->setTimeoutCount(3000);
    QSharedPointer<BaseCmd> sharedGatewayLoginCmd(gatewayLoginCmd);
    this->sendCmd(m_target,sharedGatewayLoginCmd);

}

void CloudDevice::heartBeatCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);
    AutomaticXml *automaticXml = AutomaticXml::getInstance();
    automaticXml->setBestStata(true);
    automaticXml->setBestTime(0);
}

void CloudDevice::gatewayLoginSucceedCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    m_isLoginSucceed = true;
    GatewayLoginSucceedCmd *gatewayLoginSucceedCmd = qobject_cast<GatewayLoginSucceedCmd*>(sharedCmd.data());
    UserListXml *userListXml = UserListXml::getInstance();
    QList<UserStruct> userList = gatewayLoginSucceedCmd->userList();
    userListXml->modify(userList);

    CurrentPointStore *currentPointStore = CurrentPointStore::getInstance();
    QList<ControlPointStruct> currentPointList = currentPointStore->currentPointList();
    if(!currentPointList.isEmpty())
    {
        UpdatePointCmd *updatePointCmd = new UpdatePointCmd();
        updatePointCmd->setUpdatePointList(currentPointList);
        QSharedPointer<BaseCmd> sharedUpdatePointCmd(updatePointCmd);
        sendCmd(m_target,sharedUpdatePointCmd);
    }

//    DownloadPointListRequestCmd *downloadPointListRequestCmd = new DownloadPointListRequestCmd();
//    QSharedPointer<BaseCmd> sharedDownloadPointListRequestCmd(downloadPointListRequestCmd);
//    this->sendCmd(m_target,sharedDownloadPointListRequestCmd);

    DownloadCustomSceneRequestCmd *downloadCustomSceneRequestCmd = new DownloadCustomSceneRequestCmd();
    QSharedPointer<BaseCmd> sharedDownloadCustomSceneRequestCmd(downloadCustomSceneRequestCmd);
    this->sendCmd(m_target,sharedDownloadCustomSceneRequestCmd);

    DownloadAutomaticRequestCmd *downloadAutomaticRequestCmd = new DownloadAutomaticRequestCmd();
    QSharedPointer<BaseCmd> sharedDownloadAutomaticRequestCmd(downloadAutomaticRequestCmd);
    this->sendCmd(m_target,sharedDownloadAutomaticRequestCmd);

//      GatewayUserStataCmd *gatewayUserStataCmd = new GatewayUserStataCmd();
//      QSharedPointer<BaseCmd> sharedGeteWayUserStataRequestCmd(gatewayUserStataCmd);
//      this->sendCmd(m_target,sharedGeteWayUserStataRequestCmd);

//    DownloadFileRequestCmd *downloadPointFileRequestCmd = new DownloadFileRequestCmd();
//    downloadPointFileRequestCmd->setFileType(PointFile);
//    QSharedPointer<BaseCmd> sharedDownloadPointFileRequestCmd(downloadPointFileRequestCmd);
//    this->sendCmd(m_target,sharedDownloadPointFileRequestCmd);

    heartbeatTimer->setInterval(m_heartbeatIntervalTime);
    heartbeatTimer ->start();

}

void CloudDevice::downloadPointListCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    DownloadPointListCmd *downloadPointListCmd = qobject_cast<DownloadPointListCmd*>(sharedCmd.data());
    CurrentPointStore *currentPointStore = CurrentPointStore::getInstance();
    QList<ControlPointStruct> downloadPointList = downloadPointListCmd->downloadPointList();
    currentPointStore->insert(downloadPointList);
}

void CloudDevice::downloadCustomSceneCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    DownloadCustomSceneCmd *downloadCustomSceneCmd = qobject_cast<DownloadCustomSceneCmd*>(sharedCmd.data());
    quint16 sceneIndex = downloadCustomSceneCmd->sceneIndex();
    CustomSceneStruct customScene = downloadCustomSceneCmd->customScene();

    CustomSceneXml *customSceneXml = CustomSceneXml::getInstance();
    if(sceneIndex == 0)
    {
        customSceneXml->clear();
    }
    customSceneXml->modify(customScene);


}

void CloudDevice::updateCustomSceneCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);
    DownloadCustomSceneRequestCmd *downloadCustomSceneRequestCmd = new DownloadCustomSceneRequestCmd();
    QSharedPointer<BaseCmd> sharedDownloadCustomSceneRequestCmd(downloadCustomSceneRequestCmd);
    this->sendCmd(m_target,sharedDownloadCustomSceneRequestCmd);
}

void CloudDevice::customSceneControlCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    qDebug()<<"[CloudDevice::customSceneControlCmdHandle] ";
    CustomSceneControlCmd *customSceneControlCmd = qobject_cast<CustomSceneControlCmd*>(sharedCmd.data());
    quint64 sceneId = customSceneControlCmd->sceneId();
    CustomSceneXml *customSceneXml = CustomSceneXml::getInstance();
    QList<ActionStruct> actionList = customSceneXml->actionList(sceneId);

    if(!actionList.isEmpty())
    {
        QList<ControlPointStruct> sourcePointList;
        for(int i=0; i<actionList.size(); i++)
        {
            ActionStruct action = actionList.at(i);
            ControlPointStruct sourcePoint;
            int address = action.address();
            int value   = action.functionValue();
            quint64 delaytime=action.delayTime()/100;
            sourcePoint.setAddress(address);
            sourcePoint.setValue(value);
            sourcePoint.setDelayTime(delaytime);
            sourcePointList.append(sourcePoint);
        }

        ControlPointFunc *pointControlFunc = new ControlPointFunc();
        pointControlFunc->setSourcePointList(sourcePointList);
        QSharedPointer<BaseFunc> sharedPointControlFunc(pointControlFunc);
        emit transferFuncSignal(sharedPointControlFunc);
    }
}

void CloudDevice::downloadAutomaticCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    DownloadAutomaticCmd *downloadAutomaticCmd = qobject_cast<DownloadAutomaticCmd*>(sharedCmd.data());
    quint16 automaticIndex = downloadAutomaticCmd->automaticIndex();
    AutomaticStruct automatic = downloadAutomaticCmd->automatic();

    AutomaticXml *automaticXml = AutomaticXml::getInstance();
    if(automaticIndex == 0)
    {
        automaticXml->clear();
    }
    automaticXml->modify(automatic);
}

void CloudDevice::updateAutomaticCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);
    DownloadAutomaticRequestCmd *downloadAutomaticRequestCmd = new DownloadAutomaticRequestCmd();
    QSharedPointer<BaseCmd> sharedDownloadAutomaticRequestCmd(downloadAutomaticRequestCmd);
    this->sendCmd(m_target,sharedDownloadAutomaticRequestCmd);
}

void CloudDevice::updatePointCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    UpdatePointCmd *updatePointCmd = qobject_cast<UpdatePointCmd*>(sharedCmd.data());
    QList<ControlPointStruct> updatePointList = updatePointCmd->updatePointList();
    CurrentPointStore *currentPointStore = CurrentPointStore::getInstance();
    currentPointStore->insert(updatePointList);
}

void CloudDevice::downloadFileCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    DownloadFileCmd *downloadFileCmd = qobject_cast<DownloadFileCmd*>(sharedCmd.data());
    quint8 fileType = downloadFileCmd->fileType();
    QString url = downloadFileCmd->url();
    QMap<QString,QString> paramMap = downloadFileCmd->paramMap();

    DownloadFileFunc *downloadFileFunc = new DownloadFileFunc();
    downloadFileFunc->setFileType(fileType);
    downloadFileFunc->setUrl(url);
    downloadFileFunc->setParamMap(paramMap);
    downloadFileFunc->setSource(source);
    QSharedPointer<BaseFunc> sharedDownloadFileFunc(downloadFileFunc);
    emit transferFuncSignal(sharedDownloadFileFunc);
}

void CloudDevice::rebootCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);
    System *system = System::getInstance();
    system->rebootSystem();
}

void CloudDevice::downloadLogFileCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    Q_UNUSED(sharedCmd);
    qDebug()<<"~~~~~~downloadLogFileCmdHandle";
    MacXml *macXml = MacXml::getInstance();


    loginHttp(macXml->mac(),"jj");


}

void CloudDevice::controlPointCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    CloudControlPointCmd *cloudControlPointCmd = qobject_cast<CloudControlPointCmd*>(sharedCmd.data());

    int address = cloudControlPointCmd->address();
    int value = cloudControlPointCmd->value();
    qDebug()<<"controlPointCmdHandle addr value"<<address<<":"<<value;
    ControlPointFunc *pointControlFunc = new ControlPointFunc();
    ControlPointStruct sourcePoint;
    sourcePoint.setAddress(address);
    sourcePoint.setValue(value);
    SavePointXml *savePointXml =SavePointXml::getInstance();

    savePointXml->SavePointData(address,value);
    QString stata="Read-Cloud";
    savePointXml->JoinSystemList(address,value,stata);
    QList<ControlPointStruct> sourcePointList;
    sourcePointList.append(sourcePoint);

    pointControlFunc->setSourceDevice(this->name());
    pointControlFunc->setSourcePointList(sourcePointList);
    QSharedPointer<BaseFunc> sharedPointControlFunc(pointControlFunc);
    emit transferFuncSignal(sharedPointControlFunc);
}


void CloudDevice::heartbeatCmdTimeoutHandle(PortStruct target, QSharedPointer<BaseCmd> sharedCmd)
{
    qDebug()<<"HeartbeatCmd timeout handle!!!";
    Q_UNUSED(target);
    Q_UNUSED(sharedCmd);
    reconnectTcpSrever();
}


void CloudDevice::updatePointFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
    if(!m_isLoginSucceed)
    {
        return;
    }

    UpdatePointFunc *updatePointFunc = qobject_cast<UpdatePointFunc*>(sharedFunc.data());
    QList<ControlPointStruct> updatePointList;
    updatePointList = updatePointFunc->updatePointList();
    if(updatePointList.isEmpty())
    {
        return;
    }
    UpdatePointCmd *updatePointCmd = new UpdatePointCmd();
    updatePointCmd->setUpdatePointList(updatePointList);
    QSharedPointer<BaseCmd> sharedUpdatePointCmd(updatePointCmd);
    sendCmd(m_target,sharedUpdatePointCmd);
}

void CloudDevice::downloadFileResultFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
    DownloadFileResultFunc *downloadFileResultFunc = qobject_cast<DownloadFileResultFunc*>(sharedFunc.data());
    int result = downloadFileResultFunc->result();
    PortStruct target = downloadFileResultFunc->target();

    if(target == m_target)
    {
        DownloadFileResultCmd *downloadFileResultCmd = new DownloadFileResultCmd();
        downloadFileResultCmd->setResult(result);
        QSharedPointer<BaseCmd> sharedDownloadFileResultCmd(downloadFileResultCmd);
        sendCmd(m_target,sharedDownloadFileResultCmd);
    }
}

void CloudDevice::reconnectTcpSrever()
{
    m_isLoginSucceed = false;
    heartbeatTimer->stop();
    foreach (BasePort *port , m_portList)
    {
         if((port->portParam().type() == "TCPClient"))
         {
             emit closePortSignal(port->portParam());
             emit openPortSignal(port->portParam());
         }
    }
}


void CloudDevice::heartBestHandled(PortStruct target,QSharedPointer<BaseCmd> sharedCmd)
{
   // qDebug()<<"hello my friends";
    Q_UNUSED(target);   qDebug()<<"HeartBest now -1";
  HeartBestList *HeartBestlist=HeartBestList::GetInstance();
 // QList<HeartBestData> HeartBestDatalist;
  HeartBestCmd *Heartbeatcmd=qobject_cast<HeartBestCmd*>(sharedCmd.data());
  HeartBestData HeartBestdata;
  if(Heartbeatcmd->MAC().at(0)==0x4E &&Heartbeatcmd->MAC().at(1)==0x58)
  {
      QString setMACdata;
      QByteArray setMAC;
      for(int num=0;num<Heartbeatcmd->MAC().size();num++)
      {
          if(Heartbeatcmd->MAC().at(num)<0x20)
          {
              if(Heartbeatcmd->MAC().at(num)>=0x0A)
              {
                  quint8 getdata=Heartbeatcmd->MAC().at(num)+0x37;
                  setMACdata.append(getdata);
              }else
              {
                  quint8 getdata=Heartbeatcmd->MAC().at(num)+0x30;
                  setMACdata.append(getdata);
              }
          }else
          {
           quint8 getdata=Heartbeatcmd->MAC().at(num);
           setMACdata.append(getdata);
          }
      }
      setMAC=setMACdata.toUtf8();
      Heartbeatcmd->SetMAC(setMAC);

      qDebug()<<"set mac now ---------EX";
      qDebug()<<"setdata="<<DataTypeConver::formatString(setMAC);
  }
  HeartBestdata.SetMAC(Heartbeatcmd->MAC());
  HeartBestdata.SetID(Heartbeatcmd->EquimentID());
  HeartBestdata.SetErrornum(Heartbeatcmd->Errornum());
  HeartBestdata.SetProgramNum(Heartbeatcmd->ProgramNum());
  HeartBestdata.SetTypenum(Heartbeatcmd->Typenum());
  quint16 num=Heartbeatcmd->EquimentID();
  QString a=QString::number(num).toUpper();
  /*****************************************************************/
//  QString text;
//  QString currentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
//  text = currentTime +"ID="+a+"\r\n";
//  QByteArray textArray = text.toLatin1();
//  fprintf(stderr, textArray.constData());

//  QFile file(QCoreApplication::applicationDirPath() + "/ZigbeeBest.txt");
//  file.open(QIODevice::WriteOnly | QIODevice::Append);
//  QTextStream textStream(&file);
//  textStream << text;
//  file.flush();
//  file.close();
/*********************************************************************/


  foreach (const HeartBestData &meta, HeartBestlist->m_HeartBestDataList)
  {
    if(meta.MAC()==HeartBestdata.MAC())
    {
        qDebug()<<"oldMAC=="<<meta.MAC().data();
        return;
    }
  //  qDebug()<<"newMAC=="<<HeartBestdata.MAC().data();
  }

//  QString atext;
//  QString getcurrentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
//  atext = getcurrentTime +"EquimentID="+a+"\r\n MAC="+QString(Heartbeatcmd->MAC().toHex().toUpper())+"\r\n";
//  QByteArray gettextArray = atext.toLatin1();
//  fprintf(stderr, gettextArray.constData());

//  QFile afile(QCoreApplication::applicationDirPath() + "/ZigbeeMACList.txt");
//  afile.open(QIODevice::WriteOnly | QIODevice::Append);
//  QTextStream gettextStream(&afile);
//  gettextStream << atext;
//  afile.flush();
//  afile.close();



  HeartBestlist->m_HeartBestDataList.append(HeartBestdata);

  //qDebug()<<"HeartBest now -2";
}


void CloudDevice::RS485setTimeFuncHandle(QSharedPointer<BaseFunc> sharedFunc)
{
     RS485setTime *rs485SetTime = qobject_cast<RS485setTime*>(sharedFunc.data());
     Package package=rs485SetTime->getPackage();
     package.setFunction(0x4f);
     package.setId(0x01);
     QByteArray sendData= cloudProtocol->generateData(package);
     PortStruct target;
     qDebug()<<"send time now-------------";
     foreach (const BasePort *port , m_portList)
     {
          if((port->portParam().type() == "UART" && port->portParam().name() == "RS485"))
          {
              target = port->portParam();
          }
     }
     emit sendDataSignal(target,sendData);
}



